package com.geovis.emergency.spd.biz.pc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.emergency.spd.biz.pc.mapper.PcDrillKpiMapper;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillKpiService;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillKpi;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDrillKpiVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 基础模块-演练考核指标 服务实现类
 * </p>
 *
 * @author Lonsho
 * @since 2022-07-26
*/

@Service
@RequiredArgsConstructor
@Slf4j
public class PcDrillKpiServiceImpl extends ServiceImpl<PcDrillKpiMapper, PcDrillKpi> implements IPcDrillKpiService {
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateBatch(List<PcDrillKpiVO> drillKpiList) {
        AtomicInteger idx = new AtomicInteger(0);
        for (PcDrillKpiVO pcDrillKpiVO : drillKpiList) {
            PcDrillKpi pcDrillKpi = BeanUtil.toBean(pcDrillKpiVO, PcDrillKpi.class);
            pcDrillKpi.setIdx(idx.incrementAndGet());
            if (StrUtil.isNotBlank(pcDrillKpi.getId())){
                baseMapper.updateById(pcDrillKpi);
            }else {
                baseMapper.insert(pcDrillKpi);
            }
        }
    }
}
